<?php
namespace app\web\controller;
use app\web\model\GoodsModel;
use app\web\model\GoodsDiscountModel;
use app\web\model\GoodsGrabModel;
use app\web\model\GoodPintuansModel;
use app\web\model\GoodsScoreModel;
use app\web\model\GoodsTypeModel;
use app\web\model\StoreGoodsTypeModel;
use app\web\model\CategoryModel;
use app\web\model\SpecModel;
use app\web\model\SpecValueModel;
use app\web\model\ScoreModel;
use app\web\model\AgentModel;
use app\adminmall\model\BrandModel;
use app\common\controller\StoreBaseController;
use think\Db;


/**
 * 商品管理
 * Class Goods
 * @package app\web\controller
 */
class GoodsController extends StoreBaseController
{
 	protected $discount_model;
 	protected $grab_model;
	protected $pintuan_model;
	protected $goodsScore_model;
	protected $mall_category_model;
	protected $goodsType_model;
	protected $spec_model;
	protected $score_model;
	protected $agent_model;
	protected $storeGoodsType_model;
	protected $brand_model;
	protected $model;
	 
	
    protected function _initialize() {
        parent::_initialize();
		$this->model = new GoodsModel();
		$this->agent_model = new AgentModel();
		
		//商品4栏目
        $this->discount_model = new GoodsDiscountModel();
		$this->grab_model = new GoodsGrabModel();
		$this->pintuan_model = new GoodPintuansModel();
		$this->score_model = new GoodsScoreModel();
		$this->spec_model = new SpecModel();
		$this->specValue_model = new SpecValueModel();
		
		//商品分类
		$this->goodsType_model = new GoodsTypeModel();
		$this->storeGoodsType_model = new StoreGoodsTypeModel();
		
		//品牌
		$this->brand_model = new BrandModel();
		
		$goodsType_list = $this->goodsType_model->select();
		$goodsType_list=array2level($goodsType_list);
		
		$storeGoodsType_list = $this->storeGoodsType_model->where('agent_id',session('storeInfo')['id'])->select();		
		
		//店铺规格
		$spec_list = $this->spec_model->where('agent_id',session('storeInfo')['id'])->select();
		
		//当前店铺信息
		$storeInfo =  $this->agent_model->where('id',session('storeInfo')['id'])->find();
		
		$brand_list = $this->brand_model->select();
		
		$this->assign('goodsType_list',$goodsType_list);
		$this->assign('storeGoodsType_list',$storeGoodsType_list);
		$this->assign('spec_list',$spec_list);
		$this->assign('score',$storeInfo['score']);
		$this->assign('brand_list',$brand_list);
		
    }
	
    /**
     * 商品列表
	 * @param int $page
     */
    public function index($page = 1){
    	
		$type = request()->param('type');
		$goods_state = request()->param('goods_state');		
		$type_model = $type.'_model';
		$where['agent_id'] = session('storeInfo')['id'];
		$where['goods_state']= $goods_state;		
		if(request()->isPost()){
		 	$param = input('post.');
			if($param['slt_goods']){
				$where['name'] = ['like', '%'.$param['slt_goods'].'%'];
			}
			 // 添加时间
			if($param['add_time']&&$param['end_time']){
             $map['a.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];

			}
		}
		$list = $this->$type_model->where($where)->paginate(10); 
		return $this->fetch('index', ['list' => $list,'add_type'=>'add'.$type,'type'=>$type,'goods_state'=>$goods_state]);
    }
	
	 /**
	 * 	拼团商品列表
	 * @param int $page
	 */
    public function pintgoods($page = 1){
		$goods_state = request()->param('goods_state');	  
		$where['agent_id'] = session('storeInfo')['id'];
		$where['goods_state']= $goods_state;	 		
		if(request()->isPost()){
		 	$param = input('post.');
			if($param['slt_goods']){
				$where['name'] = ['like', '%'.$param['slt_goods'].'%'];
			}
		}
		$list = $this->pintuan_model->where($where)->paginate(10);
		return $this->fetch('pintgoods', ['list' => $list,'goods_state'=>$goods_state]);
    }

	/**
     *	添加拼团商品
     */
    public function addpintuan(){
        if(request()->isAjax()){
        	$param = input('post.');
			$param['add_time'] = now_time();
			$param['agent_id'] = session('storeInfo')['id'];
			if ($this->pintuan_model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Goods/pintgoods',['goods_state'=>'2']));
            }else{
                $this->error('添加失败');
            }
        }
		 return $this->fetch();
    }

       /**
     *添加疯狂抢商品
     */
    public function addGrab()
    {
        if(request()->isAjax()){
        	$param = input('post.');
			$param['add_time'] = now_time();
			$param['agent_id'] = session('storeInfo')['id'];
			if ($this->grab_model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Goods/index',['type'=>'grab','goods_state'=>'2']));
            }else{
                $this->error('添加失败');
            }
        }	
		$this->assign('ad',$this->grab_model->find(0));			
		return $this->fetch('addGrab',['type'=>'grab']);
    }
	
	/**
     *添加优惠购商品
     */
    public function addDiscount($goods_state)
    {
        if(request()->isAjax()){
        	$param = input('post.');
			$param['add_time'] = now_time();
			$param['agent_id'] = session('storeInfo')['id'];	
			if ($this->discount_model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Goods/index',['type'=>'discount','goods_state'=>'2']));
            }else{
                $this->error('添加失败');
            }
        }	
		
		$this->assign('ad',$this->discount_model->find(0));
		return $this->fetch('addDiscount',['type'=>'discount']);
    }

	/**
     * 复制其他栏目商品信息  优惠购调用方法
     */
    public function copyDiscount()
    {
		if(request()->isPost()){
	 		$params = input('post.');
			$type_model = input('post.column'); 
			$id = input('post.goodsid');
		}
		if($type_model&&$id){
			$this->assign('ad',$this->$type_model->find($id));	
		}else{
			$this->assign('ad',$this->discount_model->find(0));	
		}	
		return $this->fetch('addDiscount',['type'=>'discount','goods_state'=>'2']);
    }
	
		/**
     * 复制其他栏目商品信息  疯狂抢调用方法
     */
    public function copyGrab()
    {
		if(request()->isPost()){
	 		$params = input('post.');
			$type_model = input('post.column'); 
			$id = input('post.goodsid');
		}
		if($type_model&&$id){
			$this->assign('ad',$this->$type_model->find($id));	
		}else{
			$this->assign('ad',$this->grab_model->find(0));	
		}	
		return $this->fetch('addGrab',['type'=>'discount','goods_state'=>'2']);
    }
	
		/**
     * 复制其他栏目商品信息  赚积分调用方法
     */
    public function copyScore()
    {
		if(request()->isPost()){
	 		$params = input('post.');
			$type_model = input('post.column'); 
			$id = input('post.goodsid');
		}
		if($type_model&&$id){
			$this->assign('ad',$this->$type_model->find($id));	
		}else{
			$this->assign('ad',$this->score_model->find(0));	
		}	
		return $this->fetch('addScore',['type'=>'discount','goods_state'=>'2']);
    }
	
	/**
     *添加赚积分商品
     */
    public function addScore()
    {
        if(request()->isAjax()){
        	$param = input('post.');
			$param['add_time'] = now_time();
			$param['agent_id'] = session('storeInfo')['id'];			
			if ($this->score_model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Goods/index',['type'=>'score','goods_state'=>'2']));
            }else{
                $this->error('添加失败');
            }
        }
		
		$this->assign('ad',$this->score_model->find(0));
		return $this->fetch('addScore',['type'=>'score']);
    }
	
	/**
     *出售中的产品ON_SALE  状态为2
     */
    public function sellGoods($page = 1)
    {
		$type = request()->param('type');
		$type_model = $type.'_model';		
    	$list = $this->$type_model->where('goods_state',$model::ON_SALE)->paginate(10);		
		 if(request()->isPost()){
		 	$param = input('post.');
			$slt_goods = '%'.$param['slt_goods'].'%';
			$list = $this->$type_model->where('name', 'like', $slt_goods)->where('goods_state',$model::ON_SALE)->paginate(10);
		 }
		return $this->fetch('index', ['list' => $list,'add_type'=>'add'.$type,'type'=>$type]);
    }
	
	
	/**
     *已售罄商品SOLD_OUT  状态为3
     */
    public function sellout ($pid = '')
    {
        $type=request()->param('type');		
		$type_model = $type.'_model';		
    	$list = $this->$type_model->where('goods_state',$model::SOLD_OUT)->paginate(10);	
		if(request()->isPost()){
		 	$param = input('post.');
			$slt_goods = '%'.$param['slt_goods'].'%';
			$list = $this->$type_model->where('name', 'like', $slt_goods)->where('goods_state',$model::SOLD_OUT)->paginate(10);
		}
		return $this->fetch('index', ['list' => $list,'add_type'=>'add'.$type,'type'=>$type]);
    }
	
	
	/**
     *已下架商品OFF_THE_SHELF 状态为1
     */
    public function notSell($pid = '')
    {
        $type=request()->param('type');		
		$type_model = $type.'_model';		
    	$list = $this->$type_model->where('goods_state',$model::OFF_THE_SHELF)->paginate(10);		
		if(request()->isPost()){
		 	$param = input('post.');
			$slt_goods = '%'.$param['slt_goods'].'%';
			$list = $this->$type_model->where('name', 'like', $slt_goods)->where('goods_state',$model::OFF_THE_SHELF)->paginate(10);
		}
		return $this->fetch('index', ['list' => $list,'add_type'=>'add'.$type,'type'=>$type]);
    }
	
	
	/**
     *回收站RECYCLE_BIN 状态为1
     */
    public function deleted($pid = '')
    {
        $type=request()->param('type');		
		$type_model = $type.'_model';		
    	$list = $this->$type_model->where('goods_state',$model::RECYCLE_BIN)->paginate(10);		
		if(request()->isPost()){
		 	$param = input('post.');
			$slt_goods = '%'.$param['slt_goods'].'%';
			$list = $this->$type_model->where('name', 'like', $slt_goods)->where('goods_state',$model::RECYCLE_BIN)->paginate(10);
		}
		return $this->fetch('index', ['list' => $list,'add_type'=>'add'.$type,'type'=>$type]);
    }
	


    /**
     * [edit 编辑]
     * @return 
     */
    public function edit($id,$goods_state)
    {   	   
		$type = input('param.type');
		$type_model = $type.'_model';		
        if(request()->isAjax()){
        	$param = input('post.');  
         	if ($this->$type_model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Goods/index',['type'=>$type,'goods_state'=>$goods_state]));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->$type_model->find($id));
		$this->assign('pid',null);
		$this->assign('type',$type);
		$this->assign('goods_state',$goods_state);
        return $this->fetch();
    }
	
	 /**
     * [editpint 云拼团商品编辑]
     * @return 
     */
    public function editpint($id,$goods_state)
    {   	   
        if(request()->isAjax()){
        	$param = input('post.');  
         	if ($this->pintuan_model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Goods/pintgoods',['goods_state'=>$goods_state]));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->pintuan_model->find($id));
		$this->assign('pid',null);
		$this->assign('goods_state',$goods_state);
        return $this->fetch();
    }
	
	 /**
     * [edit 活动]
     * @return 
     */
    public function active($id,$goods_state)
    {   	   
		$type = input('param.type');
		$type_model = $type.'_model';		
        if(request()->isPost()){
        	$param = input('post.');  
			$param['limit1'] = input('limit1') ? true : false;
			$param['limit2'] = input('limit2') ? true : false;
			$param['limit3'] = input('limit3') ? true : false;
         	if ($this->$type_model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Goods/index',['type'=>$type,'goods_state'=>$goods_state]));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
		$goods = $this->$type_model->find($id);
        $this->assign('ad', $goods);
		$this->assign('goods',$goods['name']);
		$this->assign('type',$type);
		$this->assign('goods_state',$goods_state);
        return $this->fetch();
    }

    /**
     * [offshelf 上下架]
     * @return 
     */
    public function offshelf($id,$goods_state)
    {
    	$param = input('post.');     
		$type = input('param.type');		
		$type_model = $type.'_model';
		$state = 2;
		//出售中、已售罄商品下架操作    1、下架  2、上架
		if($goods_state == 2 || $goods_state == 3){
			$state = 1;
		}
     	if ($this->$type_model->allowField(true)->save(['goods_state'=>$state],['id'=>$id]) !== false) {
            $this->success('修改成功', url('Goods/index',['type'=>$type,'goods_state'=>$goods_state]));
        } else {
            $this->error('修改失败');
        }
        $id = input('param.id');
		$this->assign('pid',null);
		$this->assign('type',$type);
		$list = $this->$type_model->where('agent_id',session('storeInfo')['id'])->where('goods_state',$state)->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }
	
	 /**
     * [recovery 回收站]
     * @return 
     */
    public function recovery($id,$goods_state)
    {
    	$param = input('post.');     
		$type = input('param.type');		
		$type_model = $type.'_model';
		//修改状态为4  回收站
     	if ($this->$type_model->allowField(true)->save(['goods_state'=>'4'],['id'=>$id]) !== false) {
            $this->success('操作成功', url('Goods/index',['type'=>$type,'goods_state'=>$goods_state]));
        } else {
            $this->error('操作失败');
        }
        $id = input('param.id');
		$this->assign('pid',null);
		$this->assign('type',$type);
		$list = $this->$type_model->where('agent_id',session('storeInfo')['id'])->where('goods_state',$goods_state)->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }
	
	
	 /**
     * [offshelfpt 云拼团上下架]
     * @return 
     */
    public function offshelfpt($id,$goods_state)
    {
    	$param = input('post.');     
		$state = 0;
		//出售中、已售罄商品下架操作  0、上架  1、下架  
		if($goods_state == 2 || $goods_state == 3){
			$state = 1;
		}
     	if ($this->pintuan_model->allowField(true)->save(['goods_state'=>$state],['id'=>$id]) !== false) {
            $this->success('修改成功', url('Goods/pintgoods',['goods_state'=>$goods_state]));
        } else {
            $this->error('修改失败');
        }
        $id = input('param.id');
		$this->assign('pid',null);
		$list = $this->pintuan_model->where('agent_id',session('storeInfo')['id'])->where('goods_state',$state)->paginate(10);
        return $this->fetch('pintgoods', ['list' => $list]);
    }
	
	 /**
     * [recoverypt 云拼团回收站]
     * @return 
     */
    public function recoverypt($id,$goods_state)
    {
    	$param = input('post.');     
		//修改状态为4  回收站
     	if ($this->pintuan_model->allowField(true)->save(['goods_state'=> '4'],['id'=>$id]) !== false) {
            $this->success('操作成功', url('Goods/pintgoods',['goods_state'=>$goods_state]));
        } else {
            $this->error('操作失败');
        }
        $id = input('param.id');
		$this->assign('pid',null);
		$list = $this->pintuan_model->where('agent_id',session('storeInfo')['id'])->where('goods_state',$goods_state)->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }
	
	
	 /**
     * [del删除云拼团]
     */
    public function deletept()
    {
        $id = input('param.id');		
        if ($this->pintuan_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }



    /**
     * [del删除]
     */
    public function delete()
    {
        $id = input('param.id');
		$type = input('param.type');
		
		$type_model = $type.'_model';
		
        if ($this->$type_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
	
	/**
     * [二维码]
     */
    public function code($id,$type,$goods_state){
		$type_model = $type.'_model';
        $shareUrl = "http://www.auth.cn/web/agent/index.html";
		$agentshareUrl = 'http://www.vshop88.net/code/code.php?url='.urlencode($shareUrl).'&margin=1&size=10';
		
		if($type == 'grab'){
			$typename = '疯狂抢商品';
		}else if($type == 'discount'){
			$typename = '优惠购商品';
		}else{
			$typename = '赚积分商品';
		}
        $id = input('param.id');
		$this->assign('type',$type);
		$this->assign('typename',$typename);
		$this->assign('goods_state',$goods_state);
		$this->assign('ad',$this->$type_model->find($id));
		$this->assign('code_url',$agentshareUrl);
		$this->assign('url',$shareUrl);
        return $this->fetch();
    }
	
	/**
     * [二维码]
     */
    public function ptcode($id,$goods_state){
        $shareUrl = "http://www.auth.cn/web/agent/index.html";
		$agentshareUrl = 'http://www.vshop88.net/code/code.php?url='.urlencode($shareUrl).'&margin=1&size=10';

        $id = input('param.id');
		$this->assign('goods_state',$goods_state);
		$this->assign('ad',$this->pintuan_model->find($id));
		$this->assign('code_url',$agentshareUrl);
		$this->assign('url',$shareUrl);
        return $this->fetch();
    }
	
	 /**
     * [商品类型]
	  * @param int
     */
    public function storegoodstype($pid)
    {
       if(request()->isAjax()){
		   if(intval($pid)){
		   		$child =$this->storeGoodsType_model->where(['pid'=> $pid,'agent_id' => session('storeInfo')['id']])->select();
			   return $child;
		   }else{
		   		return 1;
		   }
        }
        
    }
    
    /**
     * [设置已售商品数量]
	  * @param int
     */
    public function integral($id,$integral,$type) {
		$type_model = $type.'_model';
   		$member = $this->$type_model->find($id);
   		$a = $member['sales'] + $integral;
		if ($this->$type_model->allowField(true)->save(['sales'=>$a],['id'=>$id]) !== false) {
        	$this->success("操作成功");
        } else {
            $this->error('操作失败');
        }
   }
	 /**
     * [商品规格值]
	  * @param int
     */
    public function specval($spec_id)
    {
       if(request()->isAjax()){
            $param = input('post.'); 
		   if(intval($spec_id)){
		   		$child =$this->specValue_model->where('spec_id',$spec_id)->select();
			   
			   return $child;
		   }
        }
    }
}
